package org.springside.examples.miniweb.service.account;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.examples.miniweb.dao.account.CompanyDao;
import org.springside.examples.miniweb.entity.Company;

/**
 * 安全相关实体的管理类,包括用户和权限组.
 * 
 * @author calvin
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class CompanyManager {

	private CompanyDao companyDao;

	// -- User Manager --//
	public Company getCompany(Long id) {
		return companyDao.findOne(id);
	}

	@Transactional(readOnly = false)
	public void saveCompany(Company entity) {
		companyDao.save(entity);
	}

	/**
	 * 删除用户,如果尝试删除超级管理员将抛出异常.
	 */
	@Transactional(readOnly = false)
	public void deleteCompany(Long id) {
		companyDao.delete(id);
	}

	public List<Company> getAllCompany() {
		return (List<Company>) companyDao
				.findAll(new Sort(Direction.ASC, "id"));
	}

	/**
	 * @param companyDao
	 *            the companyDao to set
	 */
	@Autowired
	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}

}
